﻿/*{
author=pk2.2（HoneyBee、江东新风）
date=2023/04/24
}*/
// ## 2023/010/15 #铃 # 改写为AI指令,判决改为战斗状态,不再使用概率.同时减少大量无意义运算,简化逻辑, ##
// ## 2023/04/24 #江东新风# 修复max_devs越界问题，还未从根本解决问题 ##
// ## 2020/12/31 #江东新风# 简单翻译 ##
// ## 2020/12/12 #江东新风# 修复trace参数类型错误 ##
// ## 2020/07/26 ##
/*
@제작자: HoneyBee
@내용: 일정확률로 AI 세력의 도시방벽 자동 건설
@기타: 기마책사님의 도시방벽건설을 참고하여 제작한 SCRIPT

*/

namespace 城市屏障建设_AI
{

	class Main
	{

		Main()
		{
			pk::bind(202, pk::trigger202_t(onAIRunningOrder));
		}

		void onAIRunningOrder(pk::ai_context @context, pk::building @building, int cmd)
		{
			if (cmd == 据点AI_屏障建设 )
			{

				if(ai::base_in_range_enemy_troops_from_player[building.get_id()] > 20000)
				build_defence_wall(building);
			}
		}

		void build_defence_wall(pk::building @building)
		{

			int facility_id;
			int building_id;
			int force_id;
			pk::point building_pos;
			array<pk::point> arr;

			building_id = building.get_id();
			building_pos = building.pos;
			string building_name = pk::decode(pk::get_name(building));
			force_id = building.get_force_id();

			pk::force @force0 = pk::get_force(force_id);

			if (pk::has_tech(force0, 技巧_石造建筑))
				facility_id = 设施_石壁;
			else
				facility_id = 设施_土垒;

			int count_hex = 0;
			int range = 3 + ((building_id < 城市_末) ? 1 : 0);
			arr = pk::ring(building_pos, range);

			for (int j = 0; j < int(arr.length); j++)
			{
				pk::point pos = arr[j];
				pk::hex @hex = pk::get_hex(pos);

				// pk::trace(pk::format("building_nam:{},x:{},y:{}", building_name,pos.x,pos.y));

				bool enabled_pos = true;
				for (int dir = 0; dir < 方向_末; dir++)
				{
					pk::point chk_pos = pk::get_neighbor_pos(building_pos, dir, range);
					if (chk_pos == pos)
						enabled_pos = false;
				}

				if (!hex.has_building and !hex.has_unit and pk::is_enabled_terrain(hex.terrain) and enabled_pos and !check_develop_pos(pos) and building_id < 城市_末 and (hex.terrain <= 地形_湿地 or hex.terrain == 地形_荒地))
				{
					count_hex += 1;
				}
			}
			//如果可建设的空地<6,则认为地形不适合或者近期已经建造过,则跳出.
			if (count_hex <= 6) return;

			count_hex = 0;
			for (int j = 0; j < int(arr.length); j++)
			{
				pk::point pos = arr[j];
				pk::hex @hex = pk::get_hex(pos);

				// pk::trace(pk::format("building_nam:{},x:{},y:{}", building_name,pos.x,pos.y));

				bool enabled_pos = true;
				for (int dir = 0; dir < 方向_末; dir++)
				{
					pk::point chk_pos = pk::get_neighbor_pos(building_pos, dir, range);
					if (chk_pos == pos)
						enabled_pos = false;
				}

				if (!hex.has_building and !hex.has_unit and pk::is_enabled_terrain(hex.terrain) and enabled_pos and !check_develop_pos(pos) and building_id < 城市_末 and (hex.terrain <= 地形_湿地 or hex.terrain == 地形_荒地))
				{
					count_hex += 1;

					pk::create_building(pk::get_facility(facility_id), pos, force_id);
					pk::building @facility = pk::get_building(pos);
					pk::complete(facility);
				}
			}
			if (count_hex > 6)
				pk::history_log(building.pos, force0.color, pk::encode(pk::format("\x1b[2x{}\x1b[0x紧急建设防御屏障。", building_name)));

		} // void build_defence_wall()

		bool check_develop_pos(pk::point pos_t)
		{

			auto list = pk::list_to_array(pk::get_city_list());

			for (int i = 0; i < int(list.length); i++)
			{

				pk::city @city_ = list[i];
				int size = pk::min(city_.max_devs, city_.get_dev_size()); // 有意外情况会导致max_devs大于dev[j]
				for (int j = 0; j < size; j++)
				{
					if (city_.dev[j].pos == pos_t) // 有意外情况会导致max_devs大于dev[j]
						return true;
				}
			}
			return false;
		}

	} // class

	Main main;
}